package com.shanling.shanlingcontroller.ui.activity;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shanling.shanlingcontroller.CustomApplication;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.CommonAdapter;
import com.shanling.shanlingcontroller.adapter.ViewHolder;
import com.shanling.shanlingcontroller.base.BaseAppCompatActivity;
import com.shanling.shanlingcontroller.bean.Constants;
import com.shanling.shanlingcontroller.bean.EventCenter;
import com.shanling.shanlingcontroller.bean.Music;
import com.shanling.shanlingcontroller.music.event.PlayModeEvent;
import com.shanling.shanlingcontroller.music.player.PlayManager;
import com.shanling.shanlingcontroller.music.player.playqueue.PlayQueueManager;
import com.shanling.shanlingcontroller.utils.ConvertUtils;
import com.shanling.shanlingcontroller.utils.CoverLoader;
import com.shanling.shanlingcontroller.utils.PreferenceUtil;
import com.shanling.shanlingcontroller.view.MusicPopupWindow;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class StyleDetailActivity extends BaseAppCompatActivity implements AdapterView.OnItemClickListener, MusicPopupWindow.onListener {

    @BindView(R.id.iv_bg)
    ImageView ivBg;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_play)
    ImageView ivPlay;
    @BindView(R.id.iv_cover)
    ImageView ivCover;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_playall)
    TextView tvPlayall;
    @BindView(R.id.iv_selecter)
    ImageView ivSelecter;
    @BindView(R.id.rl_cover)
    RelativeLayout rlCover;
    @BindView(R.id.lv_songs)
    ListView lvSongs;
    private CommonAdapter<Music> mySongAdapter;
    private List<Music> musicList = new ArrayList<>();
    private MusicPopupWindow musicPopupWindow;

    @Override
    protected void getBundleExtras(Bundle extras) {
        musicList=extras.getParcelableArrayList("musicList");

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_style_detail;
    }

    @Override
    protected void initViewsAndEvents() {
        musicPopupWindow = new MusicPopupWindow(this);
        musicPopupWindow.setListener(this);
        mySongAdapter = new CommonAdapter<Music>(this, musicList, R.layout.item_song) {
            @Override
            public void convert(ViewHolder helper, int position, Music item) {
                ImageView iv_cover = helper.getView(R.id.iv_cover);
                TextView tv_songName = helper.getView(R.id.tv_song_name);
                TextView tv_albumName = helper.getView(R.id.tv_ablumname);
                ImageView iv_action = helper.getView(R.id.iv_action);
                CoverLoader.INSTANCE.loadImageView(mContext, item.getCoverUri(), iv_cover);
                tv_songName.setText(ConvertUtils.getTitle(item.getTitle()));
                tv_albumName.setText(ConvertUtils.getArtistAndAlbum(item.getArtist(), item.getAlbum()));
                if (PlayManager.getPlayingId().equals(item.getMid())) {
                    tv_songName.setTextColor(ContextCompat.getColor(mContext, R.color.text_color));
                    tv_albumName.setTextColor(ContextCompat.getColor(mContext, R.color.text_color));
                    ;
                } else {
                    tv_songName.setTextColor(ContextCompat.getColor(mContext, R.color.text_normal));
                    tv_albumName.setTextColor(ContextCompat.getColor(mContext, R.color.text_hint_color));
                }
                iv_action.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        musicPopupWindow.showPopwindow(item, iv_action);
                    }
                });
            }
        };
        lvSongs.setAdapter(mySongAdapter);
        lvSongs.setOnItemClickListener(this);
        tvName.setText(getString(R.string.unknown));
    }
    @Override
    protected void initData() {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected void onEventComing(EventCenter eventCenter) {

    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }


    @OnClick({R.id.iv_back, R.id.iv_play, R.id.tv_playall,R.id.iv_selecter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_play:
                readyGo(MusicPlayActivity.class);
                break;
            case R.id.tv_playall:
                playMusic(0);
                PreferenceUtil.getInstance(CustomApplication.getInstance()).setPlayMode(PlayQueueManager.PLAY_MODE_OREDE);
                EventBus.getDefault().post(new EventCenter(Constants.PLAYMODEEVENT, new PlayModeEvent()));
                break;
            case R.id.iv_selecter:
                Bundle bundle=new Bundle();
                bundle.putParcelableArrayList("musicList", (ArrayList<? extends Parcelable>) musicList);
                readyGo(MusicsOperatingActivity.class,bundle);
                break;

        }
    }

    public void playMusic(int position) {
        PlayManager.play(position, musicList, Constants.PLAYLIST_LOCAL_ID);
        Bundle bundle=new Bundle();
        bundle.putParcelable("music",musicList.get(position));
        readyGo(MusicPlayActivity.class,bundle);
        mySongAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        playMusic(position);
    }

    @Override
    public void onDelete(Music music) {
        musicList.remove(music);
        mySongAdapter.notifyDataSetChanged();
    }
}
